﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StringExtension.cs" company="" />
// <summary>
//   Defines extensions to the 'string' type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Utilities
{
    public static class StringExtension
    {
        /// <summary>
        /// Crops a lengthy string into a representation where showing only the prefix and postfix (sholders) of the string.
        /// </summary>
        /// <param name="input">The string to operate on.</param>
        /// <param name="threshold">The threshold length from which to cut. If the length of the input string is less, no cropping is done.</param>
        /// <param name="maxSholderLenth">When cropping - the maximum size allowed for the prefix and postfix of the input string.</param>
        public static string ToShortenedNotation(this string input, int threshold = 15, int maxSholderLenth = 15)
        {
            const string connectingString = " ... ";

            if (input.Length < threshold)
            {
                return input;
            }

            int cropLength = input.Length > (2 * maxSholderLenth) + connectingString.Length
                ? maxSholderLenth
                : (input.Length - connectingString.Length) / 2;
            string prefix = input.Substring(0, cropLength);
            string postFix = input.Substring(input.Length - cropLength);

            return prefix + connectingString + postFix;
        }

        /// <summary>
        /// Converts characters of new-line into a specified given character which is a blank space by default.
        /// </summary>
        /// <param name="input">The string to operate on.</param>
        /// <param name="replacement">An optional replacement character.</param>
        /// <returns>The modified string.</returns>
        public static string EliminateEols(this string input, char replacement = ' ')
        {
            return input.Replace('\xa', replacement).Replace('\xd', replacement);
        }
    }
}
